<template>
    <div class="real-name-auth-page">
        <!-- 1. 公用的头部导航 -->
        <common-nav-header>
            <template #left>
                <a class="close-btn fl" href="javascript:;"></a>
            </template>
            <template #title>
                <h1 class="title fl">实名认证</h1>
            </template>
            <template #right>
                <a class="auth-info fr" href="javascript:;"></a>
            </template>
        </common-nav-header>
        <!-- 2. 主体内容部分 -->
        <div class="main-content">
            <!-- 2-1. 两种实名认证方式 -->
            <div class="auth-header">
                <nav class="clearFix">
                    <a href="javascript:;">
                        <em></em>
                        <span>实名认证</span>
                    </a>
                    <a href="javascript:;">
                        <span></span>
                    </a>
                    <a href="javascript:;">
                        <em></em>
                        <span>身份认证</span>
                    </a>
                </nav>
            </div>
            <!-- 2-2. 上传身份证照片 -->
            <div class="common-upload upload-photo">
                <h2>上传身份证照片</h2>
                <div class="upload-info clearFix">
                    <van-uploader v-model="idCard_front" :before-read="beforeRead" :after-read="afterReadFrontHandle"
                        max-count="1" />
                    <van-uploader v-model="idCard_cloth" :after-read="afterReadClothHandle" max-count="1" />
                </div>
            </div>
            <!-- 2-3. 上传标准 -->
            <div class="common-upload upload-standard">
                <h2>上传标准</h2>
                <ul class="clearFix">
                    <li>
                        <img class="standard-pic" src="/images/standardPhoto.png" alt="标准照片" title="标准照片" />
                        <strong>标准照片</strong>
                    </li>
                    <li>
                        <img class="vague-pic" src="/images/standardPhoto.png" alt="模糊照片" title="模糊照片" />
                        <strong>模糊照片</strong>
                    </li>
                    <li>
                        <img class="FlashLight-pic" src="/images/standardPhoto.png" alt="闪光照片" title="闪光照片" />
                        <strong>闪光照片</strong>
                    </li>
                </ul>
            </div>
            <!-- 2-4. 底部的确定按钮 -->
            <button class="sure-btn">确定</button>
        </div>
    </div>
</template>

<script>
    // 引入公用的头部导航组件
    import CommonNavHeader from '@/components/commonHeader/CommonNavHeader';

    // 这里直接引入axios，仅仅用来测试
    import axios from 'axios';

    export default {
        name: 'realNameAuthPage',
        watch: {

        },
        created() {

        },
        mounted() {

        },
        data() {
            return {
                idCard_front: [
                    /** {
                        id: 1,
                        title: '测试照片1',
                        url: 'https://img.yzcdn.cn/vant/tree.jpg'
                    } */
                ],
                idCard_cloth: [],
                // 七牛云的上传地址，根据自己所在地区选择，我这里是华南区
                domain: 'http://upload-z2.qiniup.com',
                // 这是七牛云空间的外链默认域名
                qiniuaddr: 'q9u3hbz21.bkt.clouddn.com'
            };
        },
        methods: {
            // 验证上传文件合法性
            beforeRead(file) {
                // 定义上传图片格式
                const isJPG = file.type == 'image/jpeg' || file.type == 'image/png';

                // 定义上传图片大小
                const isLimit2M = file.size / 1024 / 1024 < 10;

                if (!isJPG) {
                    this.$toast('上传图片只能是JPG格式！');
                    return false;
                }

                if (!isLimit2M) {
                    this.$toast('上传图片大小不能超过10MB！');
                    return false;
                }

                return true;
            },
            // 上传文件到七牛云
            async afterReadFrontHandle(file) {
                file.status = 'uploading';
                file.message = '上传中...';

                const config = {
                    headers: {
                        'Content-Type': 'multipart/form-data'
                    }
                };                            

                // 上传文件类型
                let fileType = '';
                if (file.file.type == 'image/png') {
                    fileType = 'png';
                } else {
                    fileType = 'jpg';
                }

                // 重命名要上传的文件
                const keyName = 'allen_' + new Date() + Math.floor(Math.random() * 100) + '.' + fileType;
                
                // 从后端获取上传凭证
                let res = await this.$axios.upload.getUploadTokenHandle({
                    params: {}
                });

                const { responseData: { uploadToken } } = res.data;

                const formData = new FormData();

                formData.append('file', file.file);
                formData.append('token', uploadToken);
                formData.append('key', keyName);

                // 获取到凭证之后再将文件上传到七牛云空间
                let qiniu_res = await axios.post(this.domain, formData, config);

                setTimeout(() => {
                    this.idCard_front[0].url = 'http://' + this.qiniuaddr + '/' + qiniu_res.data.key;
                    file.status = 'success';
                    file.message = '上传成功';
                }, 2500);
            },
            afterReadClothHandle(file) {
                file.status = 'uploading';
                file.message = '上传中...';

                setTimeout(() => {
                    file.status = 'failed';
                    file.message = '上传失败';
                }, 1000);
            }
        },
        computed: {

        },
        components: {
            CommonNavHeader
        }
    };

</script>

<style lang="scss">
    .van-uploader {
        float: left;

        .van-uploader__upload {
            width: 308px;
            height: 176px;
            border: 1px dashed #eee;
            background-color: #fff;
            margin: 0;

            .van-icon-photograph {
                width: 64px !important;
                height: 64px !important;
                background: url(/images/wf_ico.png) no-repeat -751px -98px;
                background-size: 834px 535px;

                &::before {
                    content: '';
                }
            }

            .van-uploader__input {
                width: 100%;
                height: 176px;
            }
        }

        &:last-of-type {
            float: right;
        }

        .van-uploader__preview-image,
        .van-uploader__mask {
            width: 308px;
            height: 176px;
        }
    }

</style>

<style lang="scss" scoped>
    .real-name-auth-page {
        width: 100%;
        background-color: #f2f2f2;
        padding-bottom: 0 !important;

        .common-nav-header {
            background-color: #fff;

            .close-btn {
                width: 34px;
                height: 88px;
                background: url(/images/wf_ico.png) no-repeat -18px 16px;
                background-size: 834px 535px;
            }

            .auth-info {
                display: none;
            }
        }

        .main-content {
            width: 100%;
            background-color: #fff;

            .auth-header {
                display: flex;
                display: -webkit-flex;
                flex-direction: row;
                -webkit-flex-direction: row;
                justify-content: center;
                -webkit-justify-content: center;
                padding: 20px 82px 0;
                box-sizing: border-box;
                -webkit-box-sizing: border-box;
                margin-top: 2px;

                nav {

                    a {
                        float: left;
                        font-weight: normal;
                        font-size: 28px;
                        color: #2fe4e0;
                        white-space: nowrap;
                        text-align: center;

                        em {
                            display: block;
                            margin: 0 auto;
                            width: 72px;
                            height: 72px;
                            background: url(/images/wf_ico.png) no-repeat -559px -94px;
                            background-size: 834px 535px;
                            padding-top: 84px;
                            box-sizing: border-box;
                            -webkit-box-sizing: border-box;
                        }

                        &:last-of-type {
                            em {
                                background-position: -655px -94px;
                            }
                        }

                        &:nth-of-type(2) {
                            width: 171px;
                            height: 4px;
                            background-color: #2fe4e0;
                            margin: 34px 56px 0;
                        }
                    }
                }
            }

            .common-upload {
                padding: 0 40px;
                box-sizing: border-box;
                -webkit-box-sizing: border-box;
                margin-top: 70px;

                h2 {
                    font-weight: normal;
                    font-size: 30px;
                    color: #333;
                }
            }

            .upload-photo {
                .upload-info {
                    margin-top: 32px;
                }
            }

            .upload-standard {
                h2 {
                    margin-bottom: 40px;
                }

                ul {
                    li {
                        float: left;
                        margin-right: 20px;
                        width: 210px;

                        img {
                            width: 100%;
                            height: 128px;
                        }

                        strong {
                            display: block;
                            font-weight: normal;
                            font-size: 26px;
                            color: #333;
                            text-align: center;
                            margin: 16px 0 0 2px;

                            &::before {
                                content: '';
                                display: inline-block;
                                width: 26px;
                                height: 26px;
                                background: url(/images/wf_ico.png) no-repeat -631px -20px;
                                background-size: 834px 535px;
                                position: relative;
                                top: 4px;
                                margin-right: 12px;
                            }
                        }

                        &:nth-of-type(2),
                        &:last-of-type {
                            strong {
                                &::before {
                                    background-position: -691px -20px;
                                }
                            }
                        }

                        &:last-of-type {
                            margin-right: 0;
                        }
                    }
                }
            }

            .sure-btn {
                position: absolute;
                left: 50%;
                bottom: 45px;
                z-index: 9999;
                width: 670px;
                height: 100px;
                border-radius: 10px;
                background-color: #2fe4e0;
                font-weight: normal;
                font-size: 34px;
                line-height: 100px;
                color: #fff;
                -webkit-transform: translateX(-50%);
                -moz-transform: translateX(-50%);
                -ms-transform: translateX(-50%);
                -o-transform: translateX(-50%);
                transform: translateX(-50%);
            }
        }
    }

</style>
